package com.hoshiicloud.market.service.impl.commission;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.market.entity.commission.CommissionProfile;
import com.hoshiicloud.market.feign.IdGeneratorService;
import com.hoshiicloud.market.mapper.commission.CommissionProfileMapper;
import com.hoshiicloud.market.service.commission.CommissionProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author:campchen
 * @Date:Created in 上午 10:09 2019/7/25
 * @PACKAGENAME com.hoshiicloud.user.service.impl.commission
 * @Description
 */
@Service
public class CommissionProfileServiceImpl extends ServiceImpl<CommissionProfileMapper, CommissionProfile> implements CommissionProfileService {
    @Autowired
    private IdGeneratorService idGeneratorService;

    @Cacheable(cacheNames = "CommissionProfile", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Override
    public CommissionProfile getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "CommissionProfile", allEntries = true)
    @Override
    public boolean save(CommissionProfile entity) {
        String id = idGeneratorService.queryGeneratorId();
        entity.setId(Long.parseLong(id));
        entity.setCreatedtime(LocalDateTime.now());
        entity.setStatus(0);
        entity.setDeleted(0);
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增错误"));
        }
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "CommissionProfile", allEntries = true)
    @Override
    public boolean updateById(CommissionProfile entity) {
        entity.setUpdatedtime(LocalDateTime.now());
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改失败"));
        }
        return true;
    }

    /*@Override
    public boolean updateById(Long id, Integer sataus) {
        return b;
    }*/

    @Cacheable(cacheNames = "CommissionProfile", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#key")
    @Override
    public IPage<CommissionProfile> getPage(IPage<CommissionProfile> page, QueryWrapper<CommissionProfile> queryWrapper, String key) {
        return baseMapper.selectPage(page, queryWrapper);
    }
}
